package com.org.api.admin.controller;

import com.org.platform.model.ResultModel;
import java.io.InputStream;
import javax.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author duheng
 * @Date 2021/11/13 16:50
 */
@Slf4j
@RestController
public class IndexController {

	@GetMapping(value = {"/api/admin/index/sidebar"})
	public ResultModel sidebar() {
		try {
			Resource resource = new ClassPathResource("doc/admin/sidebar.json");
			InputStream inputStream = resource.getInputStream();
//            File jsonFile = ResourceUtils.getFile("classpath:doc/category.json");
//            String json = FileUtils.readFileToString(jsonFile, Charset.forName("utf-8"));
			String str = IOUtils.toString(inputStream, "utf-8");
			return ResultModel.newSuccess(str);
		} catch (Exception ex) {
			log.error("ex", ex);
			return ResultModel.newFail("获取文件失败");
		}
	}

	@GetMapping(value = {"/api/admin/index/table"})
	public ResultModel table(HttpServletRequest request) {
		try {
			Resource resource = new ClassPathResource("doc/admin/table.json");
			InputStream inputStream = resource.getInputStream();
			String str = IOUtils.toString(inputStream, "utf-8");
			return ResultModel.newSuccess(str);
		} catch (Exception ex) {
			log.error("ex", ex);
			return ResultModel.newFail("获取文件失败");
		}
	}

}
